package com.ssm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ssm.entity.Admin;
import com.ssm.entity.Permission;
import com.ssm.service.AdminService;
import com.ssm.service.PermissionService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Author jie_ge
 * @Date 2022/4/13
 */
@Controller
public class IndexController {
    @Reference
    private AdminService adminService;
    @Reference
    private PermissionService permissionService;

    private static final String INDEX = "frame/index";
    private static final String MAIN = "frame/main";

    /**
     * 框架首页
     * @return
     */
    @RequestMapping("/")
    public String index(Map map){
        //后续替换为当前用户id
        //Long adminId = 1L;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        Admin admin = adminService.getByUsername(user.getUsername());
        List<Permission> permissionList = permissionService.findMenuPermissionByAdminId(admin.getId());
        map.put("admin",admin);
        map.put("permissionList",permissionList);
        return INDEX;
    }

    /**
     * 框架主页
     * @return
     */
    @RequestMapping("/main")
    public String main(){
        return MAIN;
    }

    /**
     * 去登陆页面
     * @return
     */
    @RequestMapping("/login")
    public String tologin(){
        return "frame/login";
    }


    /**
     * 获取登录信息
     * @return
     */
    @GetMapping("/getInfo")
    @ResponseBody
    public Object getInfo(){
        //获取用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication.getPrincipal();
    }

    @GetMapping("/auth")
    public String auth(){
        return "frame/auth";
    }
}
